package com.netty.server.handler.base;

import com.netty.server.base.DefaultConnectionGroup;
import com.netty.server.base.TankEnvironment;
import com.netty.server.handler.*;
import io.netty.channel.ChannelHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class DefaultHandlerFactory implements HandlerFactory {

    private final TankEnvironment env;

    public DefaultHandlerFactory(TankEnvironment env) {
        this.env = env;
    }

    @Override
    public ChannelHandler newChannelInitializer() {
        return new TankChannelInitializer(this);
    }

    @Override
    public ChannelHandler newIdleStateHandler() {
        return new IdleStateHandler(env.getTankParams().getHeartIdleTime(),
                0, 0, TimeUnit.MILLISECONDS);
    }

    @Override
    public ChannelHandler newLoginHandler() {
        return new StatusHandler(new DefaultConnectionGroup());
    }

    @Override
    public ChannelHandler newTankFrameDecoder() {
        return new TankFrameDecoder();
    }

    @Override
    public ChannelHandler newBaseDecoder() {
        return new BaseDecoder();
    }

    @Override
    public ChannelHandler newRegisterHandler() {
        return new RegisterHandler(env.getRegister(), env.getDownProcessor(), env.getCmdIdGenerator());
    }

    @Override
    public ChannelHandler newByteCmdHandler() {
        return new ByteCmdHandler(env.getUpProcessor());
    }

    @Override
    public ChannelHandler newResCmdHandler() {
        return new ResCmdHandler();
    }

    @Override
    public ChannelHandler newClientAuthHandler() {
        return new LoginHandler(env.getUpProcessor(), env.getConnectionGroup());
    }
}
